import { SelfMadeDrugClient as Client, GetListPageSelfMadeDrugInput as InputGetListModel, BatchInsertOrUpdateSelfMadeDrugInput as InputInsertModel, OutputSelfMadeDrugModel as listItemModel, BatchInsertOrUpdateAgreementDrugInput as infoModel, BatchDeleteSelfMadeDrugInput as removeInsertModel } from "@/api/api_selfmadedrugclient";
import { ApiExceptionShow } from "@/framework/utils/api-exception-show";
import notify from "devextreme/ui/notify";
import { Vue } from "vue-class-component";
/*
 * @Author: LAB
 * @Date: 2021-05-26 17:02:18
 * @Description: 药品规格
 * @FilePath: \client-app\src\data-source\drug-made.ts
 */
export interface ListDataI<T> {
  data: T[],
  dataCount: number;
}
export default class DrugMadeData {
  client: Client;
  view: Vue
  constructor(client: Client, view: Vue) {
    this.client = client;
    this.view = view;
  }
  // 获取配伍禁忌
  getList(body: InputGetListModel): Promise<ListDataI<listItemModel>> {
    return this.client.getListPageSelfMadeDrug(body).then(res => {
      if (res.isSuccess) {
        console.log("res", res);
        return {
          data: res.listData,
          dataCount: res.pageInfo && res.pageInfo.count || 0
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }
  add(body: InputInsertModel) {
    return this.client.batchInsertOrUpdateSelfMadeDrug(body).then(res => {
      if (res.isSuccess) {
        notify(res.message, "success")
      } else {
        notify(res.message, "error")
      }
      return res.isSuccess
    }).catch(err => {
      console.log("adderr", err);
      ApiExceptionShow.showException(err, this.view)
      return false
    })
  }
  delete(removeInsertModel: removeInsertModel) {
    return this.client.batchDeleteSelfMadeDrug(removeInsertModel).then(res => {
      if (res.isSuccess) {
        notify("删除成功", "success")
      } else {
        notify(res.message, "error")
      }
    }), err => {
      ApiExceptionShow.showException(err, this.view)
    }
  }
  info(id: string): Promise<infoModel> {
    return this.client.getInfoAgreementDrug(id).then(res => {
      if (res.isSuccess) {
        return res.data
      } else {
        return {} as infoModel
      }
    })
  }
}